package com.starbucks.opencase.tool;

import com.starbucks.opencase.entity.entGoodsInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class toolGoodsInfo {
    @Autowired
    private JdbcTemplate con;

    /**
     * 查询物品详情
     * @param gid
     * @return
     */
    public entGoodsInfo queryInfo(String gid){
        List<entGoodsInfo> as=con.query("select * from goods_info where gid=?",new BeanPropertyRowMapper<>(entGoodsInfo.class),gid);
        if(as.isEmpty())return new entGoodsInfo();
        return as.get(0);
    }
    public int queryAllPageNum(String name){
        int n=con.queryForObject("select count(*) from goods_info where name like '%"+name+"%'", Integer.class);
        return n/30+(n%30==0?0:1);
    }
    public List<entGoodsInfo> queryList(String name,int pageNum){
        return con.query("select * from goods_info where name like '%"+name+"%' limit ?,?",new BeanPropertyRowMapper<>(entGoodsInfo.class),(pageNum-1)*30,30);
    }
}
